﻿using System;
using System.Collections.Generic;
using wojilu.Apps.Qycms.Domain;
using wojilu.Members.Interface;
using wojilu.Members.Sites.Domain;
using wojilu.Members.Sites.Service;
using wojilu.Members.Users.Domain;
using wojilu.Web.Controller.Common;
using wojilu.Web.Mvc.Attr;

namespace wojilu.Web.Controller.Admin.Qycms
{
    public class CallMainController : BaseController
    {

        public virtual IAdminLogService<SiteLog> logService { get; set; }

        public CallMainController()
        {
            logService = new SiteLogService();
        }

        private void setpublic()
        {
            User user = ctx.viewer.obj as User;
            var modelPowerGroup = PowerGroup.findById(user.GroupId);
            bind("powergroup", modelPowerGroup);
        }

        [HttpGet, HttpPost]
        public virtual void CallList()
        {
            SetPublic();
            setpublic();
            set("iframename", ctx.Get("iframename"));
            set("iframeheightwindow", ctx.GetInt("iframeheightwindow"));
            set("load", to(new AdminController().Load) + "?");

            var condition = String.Empty;
            DataPage<Calling> list = null;
            Int32 maxperpage = ctx.PostInt("MaxPerPage");
            Int32 page_id = ctx.PostInt("page_id") + 1;
            String limitkey = ctx.Get("limitkey");
            String limitclass = ctx.Get("limitclass");
            Int32 isclass = ctx.GetInt("isclass");
            switch (isclass)
            {
                case 1:
                    condition += " isclass = 1 ";
                    break;

                case 2:
                    condition += " isclass = 0 ";
                    break;
            }
            if (strUtil.IsNullOrEmpty(limitkey)) limitkey = "id";
            if (strUtil.IsNullOrEmpty(limitclass)) limitclass = "desc";
            condition += " order by " + limitkey + " " + limitclass;
            CurrentRequest.setCurrentPage(page_id);
            list = Calling.findPage(condition, maxperpage);
            if (ctx.web.postValueAll().Count == 0)
            {
                ctx.web.ResponseWrite(list.RecordCount.ToString());
                ctx.web.ResponseEnd();
            }
            bind("list", list.Results);
        }

        [HttpGet]
        public virtual void CallStyle()
        {
            var type = ctx.GetInt("type");
            var list = cdb.findById<CallType>(type);
            var build = new MyStringBuilder();
            build.AppendLine("<option value=\"0\">" + wojilu.lang.get("callmain_add_type_option") + "</option>");
            foreach (var row in list.Style)
            {
                build.AppendLine("<option value=\"" + row.Id + "\">" + row.Name + "</option>");
            }
            ctx.web.ResponseWrite(build.ToString());
            ctx.web.ResponseEnd();
        }

        [HttpGet]
        public virtual void CallAdd()
        {
            SetPublic();
            setpublic();
            target(CallAddDo);
            set("iframename", ctx.Get("iframename"));
            set("iframeheightwindow", ctx.GetInt("iframeheightwindow"));
            set("load", to(new AdminController().Load) + "?");
            set("refalse", 1);
            set("tab", "true");
            var lng = DefaultLng;
            set("lng", lng);
            var ctlist = cdb.findAll<CallType>();
            set("ctlist", ctlist);
        }

        [HttpPost]
        public virtual void CallAddDo()
        {
            var c = new Calling();
            c.pid = 50;
            c.lng = ctx.Post("lng");
            c.type = ctx.PostInt("type");
            c.style = ctx.PostInt("style");
            c.name = ctx.Post("name");
            c.code = ctx.PostHtmlAll("code");
            c.addtime = DateTime.Now;
            c.isclass = 1;
            Result result = c.insert();
            if (result.HasErrors)
            {
                logService.Add((User)ctx.viewer.obj, lang("callmain_add_log") + lang("log_extra_no"), "{Erros:'" + result.ErrorsText + "'}", "", ctx.Ip);
                ctx.web.ResponseWrite("false");
            }
            else
            {
                logService.Add((User)ctx.viewer.obj, lang("callmain_add_log") + lang("log_extra_ok"), "{Id:" + c.Id + ", Name:'" + c.name + "'}", "", ctx.Ip);
                ctx.web.ResponseWrite("true");
            }
            ctx.web.ResponseEnd();
        }

        [HttpGet]
        public virtual void CallEdit(Int64 Id)
        {
            SetPublic();
            setpublic();
            target(CallEditDo, Id);
            set("iframename", ctx.Get("iframename"));
            set("iframeheightwindow", ctx.GetInt("iframeheightwindow"));
            set("load", to(new AdminController().Load) + "?");
            set("type", "edit");
            Calling a = Calling.findById(Id);
            bind("read", a);
            var ctlist = cdb.findAll<CallType>();
            set("ctlist", ctlist);
            var c = cdb.findById<CallType>(a.type);
            bind("style", c.Style);
        }

        [HttpPost]
        public virtual void CallEditDo(Int64 Id)
        {
            var c = Calling.findById(Id);
            c.name = ctx.Post("name");
            c.type = ctx.PostInt("type");
            c.style = ctx.PostInt("style");
            c.code = ctx.PostHtmlAll("code");
            Result result = c.update();
            if (result.HasErrors)
            {
                logService.Add((User)ctx.viewer.obj, lang("callmain_edit_log") + lang("log_extra_no"), "{Erros:'" + result.ErrorsText + "'}", "", ctx.Ip);
                ctx.web.ResponseWrite("false");
            }
            else
            {
                logService.Add((User)ctx.viewer.obj, lang("callmain_edit_log") + lang("log_extra_ok"), "{Id:" + c.Id + ", Name:'" + c.name + "'}", "", ctx.Ip);
                ctx.web.ResponseWrite("true");
            }
            ctx.web.ResponseEnd();
        }

        [HttpPost]
        public virtual void CallDel()
        {
            string Ids = ctx.PostIdList("selectinfoid");
            string condition = string.Format("Id in ({0})", Ids);
            if (db.deleteBatch<Calling>(condition) > 0)
            {
                logService.Add((User)ctx.viewer.obj, lang("callmain_del_log") + lang("log_extra_ok"), "{Id:" + Ids + "'}", "", ctx.Ip);
                ctx.web.ResponseWrite("true");
            }
            else
            {
                logService.Add((User)ctx.viewer.obj, lang("callmain_del_log") + lang("log_extra_no"), "{Id:" + Ids + "'}", "", ctx.Ip);
                ctx.web.ResponseWrite("false");
            }
            ctx.web.ResponseEnd();
        }

        [HttpPost]
        public virtual void CallSort()
        {
            var infoid = ctx.PostIdList("infoid");
            var pid = ctx.PostIdList("pid");
            var strArray = infoid.Split(new char[] { ',' });
            var strArray1 = pid.Split(new char[] { ',' });
            for (int i = 0; i < strArray.Length; i++)
            {
                string condition = string.Format("Id = {0}", strArray[i]);
                db.updateBatch<Calling>("set pid=" + strArray1[i], condition);
            }
            logService.Add((User)ctx.viewer.obj, lang("callmain_log_sort") + lang("log_extra_ok"), "{Id:" + infoid + "'}", "", ctx.Ip);
            ctx.web.ResponseWrite("true");
            ctx.web.ResponseEnd();
        }

        [HttpPost]
        public virtual void CallSetting()
        {
            string Ids = ctx.PostIdList("selectinfoid");
            string condition = string.Format("Id in ({0})", Ids);
            var value = ctx.Post("value");
            var dbname = ctx.Post("dbname");
            db.updateBatch<Calling>("set " + dbname + "=" + value, condition);
            if (!ctx.HasErrors)
            {
                logService.Add((User)ctx.viewer.obj, lang("callmain_log_isopen") + lang("log_extra_ok"), "{Id:" + Ids + "'}", "", ctx.Ip);
                ctx.web.ResponseWrite("true");
            }
            else
            {
                logService.Add((User)ctx.viewer.obj, lang("callmain_log_isopen") + lang("log_extra_no"), "{Id:" + Ids + "'}", "", ctx.Ip);
                ctx.web.ResponseWrite("false");
            }
            ctx.web.ResponseEnd();
        }

    }
}
